package com.ofps.forms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ofps.forms.common.utils.R;
import com.ofps.forms.common.utils.TokenUtil;
import com.ofps.forms.mapper.QuestionListboxMapper;
import com.ofps.forms.domain.QuestionListbox;
import com.ofps.forms.service.QuestionListboxService;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.ofps.common.utils.SecurityUtils.getUserId;


@Service("questionListboxService")
public class QuestionListboxServiceImpl extends ServiceImpl<QuestionListboxMapper, QuestionListbox> implements QuestionListboxService {

    @Resource
    private QuestionListboxMapper questionListboxMapper;

    @Override
    public R save(HttpServletRequest request, QuestionListbox questionListbox) {
//        Integer userId = TokenUtil.getId(request);
        Integer userId = Math.toIntExact(getUserId());
        questionListbox.setUserId(userId);
        //调用接口保存
        try {
            if (this.save(questionListbox))
                return R.ok("创建填空题成功！");
        } catch (Exception e) {
            return R.error("创建失败！");
        }
        return R.error("创建失败！");
    }

    @Override
    public R delete(Map<String, Object> body) {
        QueryWrapper<QuestionListbox> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("id"));
        int delete = questionListboxMapper.delete(queryWrapper);
        if (delete == 1)
            return R.ok("删除成功！");
        else
            return R.error("删除失败！");
    }

    @Override
    public List<QuestionListbox> queryQuestionListbox(Map<String, Object> body) {
        QueryWrapper<QuestionListbox> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("question_id"));
        return questionListboxMapper.selectList(queryWrapper);
    }
}
